package com.jeasonzhao.commons.db;

import java.util.Date;

public class SqlParameter
{
    private int m_nIndexId = -1;
    private String m_strName = null;

    private Object m_value = null;
    private int m_nSqlType = java.sql.Types.VARCHAR;
    private boolean m_isSet = false;
    private boolean m_isDbNull = false;
    private SqlParameter()
    {

    }

    public SqlParameter(String strName)
    {
        this.m_strName = strName;
    }

    public SqlParameter(int nIndex)
    {
        this.m_nIndexId = nIndex;
    }

    public SqlParameter setName(String s)
    {
        this.m_strName = s;
        return this;
    }

    public SqlParameter(String strName,int nType,Object value)
    {
        this.m_strName = strName;
        this.setValue(value,nType);
    }

    public SqlParameter(String strName,Object value)
    {
        this.m_strName = strName;
        int nType = getTypeFromClass(null == value ? null : value.getClass());
        this.setValue(value,nType);
    }

    public SqlParameter(int nIndexId,int nType,Object value)
    {
        this.m_nIndexId = nIndexId;
        this.setValue(value,nType);
    }

    public static int getTypeFromClass(Class<?> type)
    {
        int n = java.sql.Types.VARCHAR;
        if(null == type)
        {
            return n;
        }
        if(Boolean.class.isAssignableFrom(type) || type.equals(Boolean.TYPE))
        {
            n = java.sql.Types.BOOLEAN;
        }
        else if(Integer.class.isAssignableFrom(type) || type.equals(Integer.TYPE))
        {
            n = java.sql.Types.INTEGER;
        }
        else if(Long.class.isAssignableFrom(type) || type.equals(Long.TYPE))
        {
            n = java.sql.Types.NUMERIC;
        }
        else if(Float.class.isAssignableFrom(type) || type.equals(Float.TYPE))
        {
            n = java.sql.Types.FLOAT;
        }
        else if(Byte.class.isAssignableFrom(type) || type.equals(Byte.TYPE))
        {
            n = java.sql.Types.TINYINT;
        }
        else if(Character.class.isAssignableFrom(type) || type.equals(Character.TYPE))
        {
            n = java.sql.Types.CHAR;
        }
        else if(Double.class.isAssignableFrom(type) || type.equals(Double.TYPE))
        {
            n = java.sql.Types.DOUBLE;
        }
        else if(Date.class.isAssignableFrom(type))
        {
            n = java.sql.Types.DATE;
        }
        else if(String.class.isAssignableFrom(type))
        {
            n = java.sql.Types.VARCHAR;
        }
        else
        {
            n = java.sql.Types.BLOB;
        }
        return n;
    }

    public static SqlParameter from(int nIndexId,Object value)
    {
        SqlParameter ret = new SqlParameter();
        ret.m_nIndexId = nIndexId;
        if(null != value && value.getClass().equals(java.util.Date.class))
        {
            value = new java.sql.Date(((java.util.Date) value).getTime());
        }
        ret.setValue(value,null == value ? java.sql.Types.VARCHAR : getTypeFromClass(value.getClass()));
        if(null == value)
        {
            ret.setDbNull(ret.m_nSqlType);
        }
        return ret;
    }

    public static SqlParameter from(String strName,Object value)
    {
        SqlParameter ret = new SqlParameter();
        ret.m_strName = strName;
        ret.setValue(value,null == value ? java.sql.Types.VARCHAR : getTypeFromClass(value.getClass()));
        return ret;
    }

    public static SqlParameter createDbNull(String strName,int nType)
    {
        SqlParameter ret = new SqlParameter();
        ret.m_isDbNull = true;
        ret.m_strName = strName;
        ret.m_nSqlType = nType;
        return ret;
    }

    public String getName()
    {
        return m_strName == null ? "" : m_strName;
    }

    public SqlParameter setValue(Object value,int sqlType)
    {
        m_value = value;
        m_nSqlType = sqlType;
        m_isSet = true;
        m_isDbNull = false;
        return this;
    }

    public SqlParameter setDbNull(int sqlType)
    {
        m_nSqlType = sqlType;
        m_value = null;
        m_isSet = true;
        m_isDbNull = true;
        return this;
    }

    public boolean isDbNull()
    {
        return m_isDbNull;
    }

    public boolean isSet()
    {
        return m_isSet;
    }

    public Object getValue()
    {
        return m_value;
    }

    public int getSqlType()
    {
        return m_nSqlType;
    }

    public int getIndexId()
    {
        return m_nIndexId;
    }

    public void cleanValue()
    {
        this.m_isSet = false;
        this.m_isDbNull = false;
        this.m_value = null;
    }

    public SqlParameter setIndexId(int nId)
    {
        this.m_nIndexId = nId;
        return this;
    }

    public SqlParameter setSqlType(int nId)
    {
        this.m_nSqlType = nId;
        return this;
    }

    public void copyValue(SqlParameter p)
    {
        if(null != p)
        {
            this.m_isSet = true;
            this.m_isDbNull = p.m_isDbNull;
            this.m_value = p.m_value;
            this.m_nSqlType = p.m_nSqlType;
        }
    }

    public static SqlParameter createDbNull(int n)
    {
        SqlParameter ret = new SqlParameter();
        ret.m_nIndexId = n;
        ret.m_isDbNull = true;
        return ret;

    }

    public String toString()
    {
        return this.m_strName + "=" + this.getValue();
    }

}
